package net.zxjava.gateway.filters;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

public class AccessHeaderFilter extends ZuulFilter {

	private static final String PREFIX = "/zxjava-gateway";

	@Override
	public String filterType() {
		return "pre";
	}

	@Override
	public int filterOrder() {
		return 2;
	}

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public Object run() {

		RequestContext requestContext = RequestContext.getCurrentContext();

		HttpServletRequest request = requestContext.getRequest();

		String resUrl = request.getServletPath();

		if (!StringUtils.startsWithIgnoreCase(resUrl, PREFIX)) {
			return null;
		}

		Integer accountId = (Integer) requestContext.get("accountId");
		if (accountId != null) {
			requestContext.addZuulRequestHeader("token", "adfs");
		}

		return null;
	}

}
